from utils.sql_utils import SQLManager
from utils.time_utils import TimeUtils
from utils.id_utils import IdUtils
from flask import request, jsonify

class FollowService:

    # 关注置顶用户
    def follow_user(request):
        user_id = request.get_json().get('user_id')
        followee_id = request.get_json().get('followee_id')
        
        # 检查
        is_follow = FollowService.is_follow(request)
        
        if is_follow['count'] > 0:
            return {'isFolloee':False}
        
        # 生成唯一ID和时间戳
        unique_id = IdUtils.generate_id()
        current_time = TimeUtils.get_current_time()
        
        # 插入新的关注记录
        sql_insert = """
            INSERT INTO rb_note_follows (
                unique_id, user_id, followee_id, created_at, modify_time
            ) VALUES (%s, %s, %s, %s, %s);
        """
        values_insert = (unique_id, user_id, followee_id, current_time, current_time)
        SQLManager().insert(sql_insert, values_insert)
        
        return {'isFolloee':True}

    # 取消关注
    def unfollow_user(request):
        user_id = request.get_json().get('user_id')
        followee_id = request.get_json().get('followee_id')
        
        # 检查
        is_follow = FollowService.is_follow(request)
        
        if is_follow['count'] == 0:
            return {'isFolloee':False}
        
        # 更新关注记录为已删除
        sql_update = """
            UPDATE 
                rb_note_follows 
            SET 
                is_deleted = 1,
                modify_time = %s
            WHERE 
                user_id = %s 
            AND 
                followee_id = %s;
        """
        current_time = TimeUtils.get_current_time()
        values_update = (current_time, user_id, followee_id)
        SQLManager().insert(sql_update, values_update)
        
        return {'isFolloee':True}
    
    def get_following_list_by_user_id(request):
        user_id = request.get_json().get('user_id')
        current = request.get_json().get('current')
        sql = """
            SELECT 
                rau.unique_id AS user_id,
                rau.admin_user_name AS user_name,
                rau.avatar_url AS user_avatar_url
            FROM 
                rb_note_follows rnf
            JOIN 
                rb_admin_user rau
            ON 
                rnf.followee_id = rau.unique_id
            WHERE 
                rnf.user_id = %s
            AND 
                rnf.is_deleted = 0;
        """
        values = (user_id,)
        
        # 执行查询
        result = SQLManager().get_list_by_page(sql, values, int(current))
        
        return result

    def get_following_list(request):
        current = request.get_json().get('current')
        print('current',current)
        sql = """
            SELECT 
                rau.unique_id AS followee_id,
                rau.admin_user_name AS user_name,
                rau.avatar_url AS user_avatar_url,
                rnf.user_id AS user_id
            FROM 
                rb_note_follows rnf
            JOIN 
                rb_admin_user rau
            ON 
                rnf.followee_id = rau.unique_id
            WHERE 
                rnf.is_deleted = 0;
        """
        
        # 执行查询
        result = SQLManager().get_list_by_page(sql,current=current)
        
        return result
    

    def is_follow(request):
        user_id = request.get_json().get('user_id')
        followee_id = request.get_json().get('followee_id')
        # 检查是否已经关注
        sql_check = """
            SELECT 
                COUNT(*) AS count
            FROM 
                rb_note_follows
            WHERE 
                user_id = %s
            AND
                followee_id = %s
            AND
                is_deleted = 0;
        """
        values_check = (user_id, followee_id)
        result_check = SQLManager().get_one(sql_check, values_check)

        return result_check

    def get_following_lists_count(followee_id):
        # 检查是否已经关注
        sql_check = """
            SELECT 
                COUNT(*) AS followee_count
            FROM 
                rb_note_follows
            WHERE 
                followee_id = %s
            AND
                is_deleted = 0;
        """
        values_check = (followee_id,)
        result_check = SQLManager().get_one(sql_check, values_check)

        return result_check
    